package DBTest;

import java.sql.SQLException;
import java.util.ArrayList;

import org.junit.Test;

import ClientDataBase.ClientDBUtils;

import records.ReturnRecord;
import records.SalesRecord;
import records.StockInRecord;
import records.StockOutRecord;
import strategy.TotalPriceDiscountStrategy;
import strategy.TotalPriceGiftStrategy;
import stuff.Goods;
import stuff.VIPCustomer;
import stuff.Worker;
import static org.junit.Assert.*;

public class ClientDB {
	ClientDBUtils handler = new ClientDBUtils();
	
	
	
	
	@Test//ok
	public void testGetWorkerInfo(){
		Worker worker = null;
		try {
			worker = handler.getWorkerInfo("AB2011002");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		assertEquals("AB2011002",worker.getIDNumber());
		assertEquals("Henry",worker.getName());
		assertEquals("tiancai",worker.getPassword());
		assertEquals(0,worker.getPosition());
	}
	
	@Test//ok
	public void testGetSalesRecord(){
		SalesRecord sr = null;
		try {
			sr = handler.getSalesRecord(1);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		//(saler_id,total_value,goods_list_id,date)values("AB2011001",120,1,"2012-11-11");
		assertEquals("2012-11-11",sr.getDate());
		assertEquals(1,sr.getRecordNumber());
		assertEquals("AB2011001",sr.getSalerID());
		System.out.print(sr.getSaleslist());
		assertEquals(120,sr.getTotalValue(),0.0);
	}
	
	
	@Test//ok
	public void testAddVIP(){
		VIPCustomer vip = new VIPCustomer();
		try {
			handler.addVIP(vip);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	
	@Test//OK
	public void testAddReturnedGoods(){
		ArrayList<Goods> list = new ArrayList<Goods>();
		Goods g1;
		Goods g2;
		try {
			g1 = handler.getGoodsInfo("1");
			g2 = handler.getGoodsInfo("2");
			g1.setNumber(3);
			g2.setNumber(5);
			list.add(g1);
			list.add(g2);
			handler.addReturnedGoods(list);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	@Test//ok
	public void testAddSalesRecord(){
		SalesRecord sr = new SalesRecord();
		ArrayList<Goods> list = new ArrayList<Goods>();
		Goods g1;
		Goods g2;
		try {
			g1 = handler.getGoodsInfo("1");
			g2 = handler.getGoodsInfo("2");
			list.add(g1);
			list.add(g2);
			sr.setSaleslist(list);
			handler.addSalesRecord(sr);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	@Test//ok
	public void testAddReturnRecord(){
		ReturnRecord rr = new ReturnRecord();
		ArrayList<Goods> list = new ArrayList<Goods>();
		Goods g1;
		Goods g2;
		try {
			g1 = handler.getGoodsInfo("2");
			g2 = handler.getGoodsInfo("1");
			list.add(g1);
			list.add(g2);
			rr.setSaleslist(list);
			handler.addReturnRecord(rr);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	
	@Test
	public void testAddStockIn(){
		StockInRecord sInRecord = new StockInRecord();
		ArrayList<Goods> list = new ArrayList<Goods>();
		Goods g1;
		Goods g2;
		try {
			g1 = handler.getGoodsInfo("2");
			g2 = handler.getGoodsInfo("1");
			list.add(g1);
			list.add(g2);
			sInRecord.setStockInList(list);
			handler.addStockIn(sInRecord);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	@Test
	public void testAddStockOut(){
		StockOutRecord sInRecord = new StockOutRecord();
		ArrayList<Goods> list = new ArrayList<Goods>();
		Goods g1;
		Goods g2;
		try {
			g1 = handler.getGoodsInfo("2");
			g2 = handler.getGoodsInfo("1");
			list.add(g1);
			list.add(g2);
			sInRecord.setStockInList(list);
			handler.addStockOut(sInRecord);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	
	@Test
	public void testDeleteGoods(){
		ArrayList<Goods> list = new ArrayList<Goods>();
		Goods g1;
		Goods g2;
		try {
			g1 = handler.getGoodsInfo("1");
			g2 = handler.getGoodsInfo("2");
			g1.setNumber(3);
			g2.setNumber(5);
			list.add(g1);
			list.add(g2);
			handler.deleteGoods(list);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	
	
	@Test//ok
	public void testChangeGoodsInfo(){
		Goods goods = null;
		try {
			goods = handler.getGoodsInfo("1");
			goods.setDescription("a cute cup");
			handler.updateGoodsInfo(goods);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	

	
}
